define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    // del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                maintainSelected: true,
                columns: [
                    [
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'goods_images', title: __('Ordergoods.images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'goods_name', title: __('Ordergoods.goods_name'), operate: false},
                        {field: 'manystoreshop.name', title: '所屬商家', operate: false},
                        {field: 'user.nickname', title: __('User.nickname'), operate: false},
                        {field: 'user.avatar', title: __('User.avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'total_integral', title: __('Total_integral'), operate: false},
                        {field: 'delivery_price', title: __('Delivery_price'), operate:false},
                        {field: 'total_price', title: __('Total_price'), operate:false},
                        {field: 'pay_price', title: __('Pay_price'), operate:false},
                        {field: 'order_status', title: __('Order_status'), searchList: {"10":__('Order_status 10'),"20":__('Order_status 20'),"30":__('Order_status 30'),"40":__('Order_status 40'),"50":__('Order_status 50')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter:function(value,row,index){
                                    this.buttons = [
                                        {
                                            name: 'edit',
                                            text: '詳情',
                                            classname: 'btn btn-xs btn-success btn-magic btn-editone',
                                            url: 'order/edit',
                                            // confirm: '确认禁用嗎？',
                                            visible: function (row) {
                                                if ( Config.order_edit ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },{
                                            name: 'cancel',
                                            text: '取消訂單',
                                            classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                            url: 'order/cancel',
                                            confirm: '确认取消訂單嗎？',
                                            visible: function (row) {
                                                if ( Config.order_cancel && row.order_status == '10') {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        }
                                    ];
                                return Table.api.formatter.operate.call(this,value,row,index);
                            }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});